package com.bsj.travel.system.controller;

import com.bsj.travel.def.common.DTO.web.user.UserDTO;
import com.bsj.travel.def.common.DTO.web.user.UserSearchDTO;
import com.bsj.travel.def.common.VO.UserVO;
import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.security.annotation.InnerAuth;
import com.bsj.travel.security.annotation.RequiresPermissions;
import com.bsj.travel.def.common.VO.TokenUser;
import com.bsj.travel.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 用户信息
 * @date 2023/11/3
 */
@RestController
@RequestMapping(value = "/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据用户获取用户信息
     *
     * @param userName
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/info/{userName}")
    public JsonResult<TokenUser> info(@PathVariable("userName") String userName) {
        return sysUserService.getTokenUserInfo(userName);
    }

    /**
     * 登录
     *
     * @param userName
     * @param passWord
     * @return com.bsj.travel.def.global.JsonResult<com.bsj.travel.def.common.VO.TokenUser>
     * @author ljx
     * @time 2024/1/10 10:51
     */
    @GetMapping(value = "/login")
    public JsonResult<TokenUser> login(String userName, String passWord) {
        TokenUser tokenUser = sysUserService.login(userName, passWord);
        return JsonResult.success(tokenUser);
    }

    /**
     * 添加用户
     *
     * @param userDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/10 10:53
     */
    @PostMapping("/add")
    @RequiresPermissions(value = "user:operation:add")
    public JsonResult add(@Validated UserDTO userDTO) {
        return sysUserService.addUser(userDTO);
    }

    /**
     * 修改用户
     *
     * @param userDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/10 10:54
     */
    @PostMapping("/update")
    @RequiresPermissions(value = "user:operation:update")
    public JsonResult update(@Validated UserDTO userDTO) {
        return sysUserService.updateUser(userDTO);
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return com.bsj.travel.def.global.JsonResult<com.bsj.travel.def.common.VO.UserVO>
     * @author ljx
     * @time 2024/1/10 10:56
     */
    @GetMapping("/getById/{userId}")
    @RequiresPermissions(value = "user:info:getById")
    public JsonResult<UserVO> getById(@PathVariable("userId")Integer userId) {
        UserVO userVO = sysUserService.getUserInfoByUserId(userId);
        return JsonResult.success(userVO);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/10 10:57
     */
    @PostMapping("/delete/{userId}")
    @RequiresPermissions(value = "user:operation:delete")
    public JsonResult delete(@PathVariable("userId") Integer userId) {
        return sysUserService.deleteUser(userId);
    }

    /**
     * 用户分页
     *
     * @param userSearchDTO
     * @return com.bsj.travel.def.global.JsonResult<java.util.List<com.bsj.travel.def.common.VO.UserVO>>
     * @author ljx
     * @time 2024/1/10 11:06
     */
    @GetMapping("/page")
    @RequiresPermissions(value = "user:info:page")
    public JsonResult<List<UserVO>> page(@Validated UserSearchDTO userSearchDTO) {
        return sysUserService.pageUser(userSearchDTO);
    }

    /**
     * 退出登录
     *
     * @param
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/10 11:14
     */
    @GetMapping("/logout")
    public JsonResult logout() {
        return sysUserService.userLogout();
    }

    /**
     * 修改密码
     *
     * @param passWord
     * @param newPassWord
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/10 11:15
     */
    @PostMapping("updatePassWord")
    public JsonResult updatePassWord(String passWord, String newPassWord) {
        return sysUserService.updatePassWord(passWord, newPassWord);
    }
}
